

from conans import ConanFile, CMake, tools, AutoToolsBuildEnvironment
from conans import tools
import sys
import os
sys.path.append(os.environ.get('PYTHONPATH'))
from pathlib import Path
import shutil


class LibeventConan(ConanFile):
    name = "ZLIB"
    version = " 1.2.11"
    url = "https://gitee.com/mirrors/zlib.git"
    description = "OPENSSL"
    settings = "os", "compiler", "build_type", "arch"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    
    def source(self):
        my_file = Path("./pkgsrc")
        if my_file.exists():
            print("not need to update source")
        else:
            tools.rmdir("./pkgsrc")
            git = tools.Git(folder="./pkgsrc", verify_ssl=False)
            git.clone(self.url)

    def build(self):
        print(self.settings.build_type)
        cmake = CMake(self)
        print("path " + self.source_folder + " is where the package built")
        print("build env is ", tools.get_env("BUILD_ENV"))
        tools.rmdir(tools.unix_path(self.package_folder)+"/build")
        tools.mkdir(tools.unix_path(self.package_folder)+"/build")
        if tools.os_info.is_windows:
            print("current os windows")
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if tools.get_env("BUILD_ENV") == "msys":
                print("msys build")
                if tools.get_env("CONAN_SH_PATH") == "":
                    print("msys openssl build require sh.exe in the CONAN_SH_PATH enviorment")
                shutil.copy("sh.exe", tools.get_env("CONAN_SH_PATH"))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True,
                                              bashcmd="./Configure --static  --prefix=" + tools.unix_path(self.package_folder))
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make")
                tools.run_in_windows_bash(self, subsystem="msys", cwd="pkgsrc", msys_mingw=True, bashcmd="mingw32-make install")
                os.remove(tools.get_env("CONAN_SH_PATH"))  # path是文件的路径，如果这个路径是一个文件夹，则会抛出OSError的错误，这时需用用rmdir()来删除
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.run("mkdir build",cwd="pkgsrc")
                self.run("cmake -G\"Visual Studio 16 2019\" -A Win32 ..",cwd="pkgsrc/build")
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    if(self.settings.build_type == "Debug"):
                        self.run("MSBuild  ALL_BUILD.vcxproj /t:Rebuild /p:Configuration=Debug", cwd="pkgsrc/build")
                    if(self.settings.build_type == "Release"):
                            self.run("MSBuild ALL_BUILD.vcxproj /t:Rebuild /p:Configuration=Release",cwd="pkgsrc/build")
            if tools.get_env("BUILD_ENV") == "msvc32":
                self.run("mkdir build",cwd="pkgsrc")
                self.run("cmake -G\"Visual Studio 16 2019\" -A Win32 ..",cwd="pkgsrc/build")
                env_vars = tools.vcvars_dict(self)
                with tools.environment_append(env_vars):
                    if(self.settings.build_type == "Debug"):
                        self.run("MSBuild ALL_BUILD.vcxproj /t:Rebuild /p:Configuration=Debug", cwd="pkgsrc/build")
                    if(self.settings.build_type == "Release"):
                            self.run("MSBuild ALL_BUILD.vcxproj /t:Rebuild /p:Configuration=Release",cwd="pkgsrc/build")
    def package(self):
        if(self.settings.build_type == "Release"):
            self.copy("pkgsrc/*.h", dst="include", keep_path=False)
            self.copy("pkgsrc/build/Release/*.lib", dst="lib", keep_path=False)
            self.copy("pkgsrc/build/Release/*.dll", dst="lib", keep_path=False)
        if(self.settings.build_type == "Debug"):
            self.copy("pkgsrc/*.h", dst="include", keep_path=False)
            self.copy("pkgsrc/build/Debug/*.lib", dst="lib", keep_path=False)
            self.copy("pkgsrc/build/Debug/*.dll", dst="lib", keep_path=False)

    def export(self):
        print("msys build the autoconf needs sh.exe normally it is not in path")
        self.copy("sh.exe",dst="",keep_path=True)

    def package_info(self):
        self.cpp_info.name = 'zlib'
        if tools.os_info.is_windows:
            if tools.os_info.is_cygwin:
                print("cygwin build")
            if self.settings.compiler == "gcc":
                print("msys build")
                self.cpp_info.names["cmake_find_package"] = "zlib"
                self.cpp_info.names["cmake_find_package_multi"] = "zlib"
                self.cpp_info.libdirs = ['lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libz.a']  # The libs to link against
            if tools.get_env("BUILD_ENV") == "msvc64":
                self.cpp_info.names["cmake_find_package"] = "zlib"
                self.cpp_info.names["cmake_find_package_multi"] = "zlib"
                self.cpp_info.libdirs = ['lib']  # Directories where libraries can be found
                self.cpp_info.libs = ['libz.lib']  # The libs to link against
                self.cpp_info.libs = ['zlibstatic.lib']  # The libs to link against                
